package com.example.newheze.FieldMessage;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.example.newheze.R;
import com.example.newheze.Util.ArrayUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lecho.lib.hellocharts.view.LineChartView;

public class MessageActivity extends Fragment implements ViewPager.OnPageChangeListener,View.OnClickListener{
    private LineChartView[] lineChartViews;
    private MyLineChartData[] myLineChartDatas;
    private MyViewPager viewPager;
    private List<View> views;
    private ViewPagerAdapter adapter;
    private AnalogData analogData=new AnalogData();
    private LineChartViewAdapter[] lineChartViewAdapters;
    private Spinner mySpinner;
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_fieldmessage, container, false);
        //初始化view
        viewPager=new MyViewPager(this.getActivity());
        viewPager=view.findViewById(R.id.viewpager);
        views=new ArrayList<View>();
        adapter=new ViewPagerAdapter(views);

        //初始化折线图
        myLineChartDatas=new MyLineChartData[10];
        lineChartViews=new LineChartView[10];
        lineChartViewAdapters=new LineChartViewAdapter[10];

        for(int i=0;i<10;i++){
            myLineChartDatas[i]=new MyLineChartData();

        }
        /*
         在这里添加传输过来的数据，不要用AnalogData类，应创建并使用
         HistoryData类实例，并将下列的方法替换。
        示例：
        HistoryData historyData[]=new HistoryData[4];
        historyData[0].setAirHumidity();
        historyData[0].setHistoryDate();
        myLineChartDatas[0].setX(historyData[0].getAirHumidity());
        myLineChartDatas[0].setY(analogData[0].gethistoryDate());
         */
        Log.d("mybundle", "onCreateView: ");
        Bundle bundle = getActivity().getIntent().getExtras();    //得到传过来的bundle
        analogData = bundle.getParcelable("Data");//读出数据
        myLineChartDatas[0].setX(ArrayUtil.reverseArray(analogData.getDate_soil_humi1()));
        myLineChartDatas[0].setY(ArrayUtil.reverseArray(analogData.getInfo_soil_humi1()));
        myLineChartDatas[1].setX(ArrayUtil.reverseArray(analogData.getDate_soil_temp1()));
        myLineChartDatas[1].setY(ArrayUtil.reverseArray(analogData.getInfo_soil_temp1()));
        myLineChartDatas[2].setX(ArrayUtil.reverseArray(analogData.getDate_soil_elect1()));
        myLineChartDatas[2].setY(ArrayUtil.reverseArray(analogData.getInfo_soil_elect1()));
        myLineChartDatas[3].setX(ArrayUtil.reverseArray(analogData.getDate_soil_humi2()));
        myLineChartDatas[3].setY(ArrayUtil.reverseArray(analogData.getInfo_soil_humi2()));
        myLineChartDatas[4].setX(ArrayUtil.reverseArray(analogData.getDate_soil_temp2()));
        myLineChartDatas[4].setY(ArrayUtil.reverseArray(analogData.getInfo_soil_temp2()));
        myLineChartDatas[5].setX(ArrayUtil.reverseArray(analogData.getDate_soil_elect2()));
        myLineChartDatas[5].setY(ArrayUtil.reverseArray(analogData.getInfo_soil_elect2()));
        myLineChartDatas[6].setX(ArrayUtil.reverseArray(analogData.getDate_sunlight()));
        myLineChartDatas[6].setY(ArrayUtil.reverseArray(analogData.getInfo_sunlight()));
        myLineChartDatas[7].setX(ArrayUtil.reverseArray(analogData.getDate_co2()));
        myLineChartDatas[7].setY(ArrayUtil.reverseArray(analogData.getInfo_co2()));
        myLineChartDatas[8].setX(ArrayUtil.reverseArray(analogData.getDate_air_humi()));
        myLineChartDatas[8].setY(ArrayUtil.reverseArray(analogData.getInfo_air_humi()));
        myLineChartDatas[9].setX(ArrayUtil.reverseArray(analogData.getDate_air_temp()));
        myLineChartDatas[9].setY(ArrayUtil.reverseArray(analogData.getInfo_air_temp()));
        for (int i=0;i<9;i++){

        }
        for(int i=0;i<10;i++){
            lineChartViews[i]=view.findViewById(R.id.chart);
        }
        initSpinner(view);
        initData();
        return view;
    }

    private void initData(){    //为折线图添加数据
        ViewGroup.LayoutParams layoutParams=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        for(int i=0;i<10;i++){
            lineChartViews[i]=new LineChartView(this.getActivity());
            lineChartViewAdapters[i]=new LineChartViewAdapter(lineChartViews[i],myLineChartDatas[i]);
            lineChartViewAdapters[i].getAxisXLables();
            lineChartViewAdapters[i].getAxisPoints();
            lineChartViewAdapters[i].initLineChart();
            lineChartViews[i].setLayoutParams(layoutParams);
            views.add(lineChartViews[i]);
        }


        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(this);
    }


    private void initSpinner(View view){
        mySpinner = (Spinner) view.findViewById(R.id.spinner);

        //4.为下拉列表设置各种点击事件，以响应菜单中的文本item被选中了，用setOnItemSelectedListener
        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {//选择item的选择点击监听事件
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
                // 将所选mySpinner 的值带入myTextView 中
                Log.d("spinner", "onItemSelected: "+arg2);
                viewPager.setCurrentItem(arg2);
            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
    }
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    public void onPageSelected(int position) {
        setCurrentPoint(position);
    }
    private void setCurrentPoint(int position){     //为viewpager选项设置点击事件

    }
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View view) {
        //initCurrentData();
        viewPager.setCurrentItem(1);
    }
}
